SELECT
(
CASE
WHEN (MAX(T.CODE)) IS NULL
THEN CONCAT(
'${codePrefix!'HD_'}', DATE_FORMAT(NOW(), '%y%m%d'), '001'
)
ELSE CONCAT(
'${codePrefix!'HD_'}', DATE_FORMAT(NOW(), '%y%m%d'), LPAD((SUBSTR(MAX(T.CODE), - 3, 3) + 1), 3, '0')
)
END
) AS T_CODE
FROM
C1_ENTITY_CODE T
WHERE T.TYPE_CODE = :typeCode
AND T.CODE LIKE CONCAT(
'${codePrefix!'HD_'}', DATE_FORMAT(NOW(), '%y%m%d'), '%'
)
